package uk.ac.durham.tel.synergynet.ats.annotations.handlers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipse.ui.handlers.HandlerUtil;

import uk.ac.durham.tel.synergynet.ats.Activator;
import uk.ac.durham.tel.synergynet.ats.annotations.model.AnnotationSetNode;
import uk.ac.durham.tel.synergynet.ats.annotations.ui.AnnotationIntervalImpl;
import uk.ac.durham.tel.synergynet.ats.navigation.NavigatorLabelProvider;
import uk.ac.durham.tel.synergynet.ats.navigation.model.INode;
import uk.ac.durham.tel.synergynet.ats.projects.model.ProjectNode;
import uk.ac.durham.tel.synergynet.ats.subjects.model.Subject;
import uk.ac.durham.tel.synergynet.ats.subjects.model.SubjectNode;

public class MoveAnnotationHandler extends AbstractHandler implements IHandler {

	public Object execute(ExecutionEvent event) throws ExecutionException {
		final ILog logger = Activator.getDefault().getLog();

		ISelection selection = HandlerUtil.getCurrentSelection(event);
		IWorkbenchPart activePart = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getActivePart();
		if (!(selection instanceof IStructuredSelection)) {
			return null;
		}
		IStructuredSelection structSel = (IStructuredSelection) selection;
		
		@SuppressWarnings("rawtypes")
		Iterator iteratorCaptionInterval = structSel.iterator();
		while (iteratorCaptionInterval.hasNext()) {
			Object element = iteratorCaptionInterval.next();
			if (element instanceof AnnotationIntervalImpl) {
				AnnotationIntervalImpl annotationInterval = (AnnotationIntervalImpl) element;
				try {
					
					//TODO This can be moved to AnnotationSetNode
					ElementListSelectionDialog dialog = new  ElementListSelectionDialog(activePart.getSite().getShell(), new NavigatorLabelProvider());
					AnnotationSetNode annotationSetNode = annotationInterval.getOwner().getAnnotationSetNode();
					ProjectNode projectNode = (ProjectNode) annotationSetNode.getLastParent();
					
					List<INode> nodesToShow = new ArrayList<INode>();
					//Removing existing subjects from the list
					List<Subject> existingSubjects = annotationSetNode.getSubjectList();
					for (INode node : projectNode.getSubjectRootNode().getChildren()) {
						if (existingSubjects.contains(((SubjectNode)node).getResource()) && (node.getResource() != annotationInterval.getOwner().getSubject()))
							nodesToShow.add(node);
					}
					dialog.setElements(nodesToShow.toArray(new INode[]{}));
					dialog.open();
					if (dialog.getFirstResult() != null) {
						SubjectNode result = (SubjectNode) dialog.getFirstResult();
						annotationSetNode.moveAnnotation(annotationInterval.getAnnotation(), annotationInterval.getOwner().getSubject(), result.getResource());
					}
					
				} catch (Exception ex) {
					IStatus status = new Status(IStatus.ERROR,Activator.PLUGIN_ID,ex.getMessage(), ex);
					logger.log(status);
				}
				return null;
			}
		}
		
		return null;

	}

}
